<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="项目名称" prop="testplatfromProjectDescribe">
        <el-input
          v-model="queryParams.testplatfromProjectDescribe"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:project:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:project:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['project:project:remove']"
          >删除</el-button>
        </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:project:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="projectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="ID"
        align="center"
        prop="testplatfromProjectId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="项目名称"
        align="center"
        prop="testplatfromProjectDescribe"
      />
      <el-table-column label="成员人数" align="center" prop="integerNumber">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdateUser(scope.row)"
            style="padding: 0"
          >
            {{ scope.row.integerNumber }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="项目描述"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdateUser(scope.row)"
            >成员管理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:project:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:project:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="testplatfromProjectDescribe">
          <el-input
            v-model="form.testplatfromProjectDescribe"
            placeholder="请输入项目名称"
          />
        </el-form-item>
        <el-form-item label="项目描述" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入项目描述"
            maxlength="200"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户关联用户列表弹窗 -->
    <el-dialog
      title="成员管理"
      :visible.sync="projectUserDialog"
      @close="cancelHand()"
      width="1493"
      append-to-body
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddUser"
          v-hasPermi="['project:member:add']"
          >添加成员</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="singleDelete"
          @click="handleDeleteUserBatch"
          v-hasPermi="['project:member:delete']"
          >批量删除</el-button
        >
      </el-col>
      <el-table
        v-loading="loading"
        :data="testplatfromProjectmemberList"
        @selection-change="handleSelectionChangeUser"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="姓名"
          align="center"
          prop="nickName"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="邮箱" align="center" prop="email" />
        <el-table-column
          label="电话"
          align="center"
          prop="phonenumber"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['project:project:edit']"
              >修改</el-button
            > -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeleteUser(scope.row)"
              v-hasPermi="['project:member:delete']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- <pagination
      v-show="totalUser > 0"
      :total="totalUser"
      :page.sync="userParams.current"
      :limit.sync="userParams.pageSize"
      @pagination="handleUpdateUser"
    /> -->
    </el-dialog>

    <el-dialog
      title="添加成员"
      :visible.sync="addUserDialog"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="userAdd" :rules="rules" label-width="80px">
        <el-form-item label="项目人员" prop="userId">
          <el-select v-model="userAdd.userId" placeholder="请选择">
            <el-option
              v-for="item in users"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
              <span style="float: left">{{ item.nickName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.phonenumber
              }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormUserAdd">确 定</el-button>
        <el-button @click="cancelAddUser">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
  projectMemberPage,
  getAlluser,
  addUser,
  deletelUser,
  deletelUserBatch,
} from "@/api/project/project";

export default {
  name: "Project",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      userIds:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 非单个禁用
      singleDelete: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalUser: 0,
      // 项目管理表格数据
      projectList: [],
      testplatfromProjectmemberList: [],
      testplatfromProjectId:null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      projectUserDialog: false,
      addUserDialog: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        testplatfromProjectDescribe: null,
        createBy: null,
      },
      //成员列表查询参数
      userParams: {
        current: 1,
        size: 10,
        testplatfromProjectId: null,
        nickName: null,
        phonenumber:null
      },
      // 表单参数
      form: {},
      userAdd: {},
      users: [],
      // 表单校验
      rules: {
        testplatfromProjectDescribe: [
          { required: true, message: "项目名称名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "项目名称名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        userId:[
        { required: true, message: "人员不能为空", trigger: "blur" },
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目管理列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then((response) => {
        this.projectList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelHand(){
      this.getList();
    },
    //取消按钮
    cancelAddUser(){
      this.addUserDialog=false;
    },
    // 表单重置
    reset() {
      this.form = {
        testplatfromProjectId: null,
        testplatfromProjectDescribe: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.testplatfromProjectId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    //多选框选中人员数据
    handleSelectionChangeUser(selection){
      console.log("2222222")
      this.userIds = selection.map((item) => item.id);
      console.log(this.userIds);
      this.singleDelete =  !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const testplatfromProjectId = row.testplatfromProjectId || this.ids;
      getProject(testplatfromProjectId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目管理";
      });
    },
    // 成员列表展示
    handleUpdateUser(row) {
      this.loading = true;
      this.userParams.testplatfromProjectId = row.testplatfromProjectId;
      console.log("33333333333333333333333")
      projectMemberPage(this.userParams).then((response) => {
        this.projectUserDialog = true;
        this.testplatfromProjectId=row.testplatfromProjectId;
        this.testplatfromProjectmemberList = response.data.records;
        this.totalUser=response.data.total;
        this.loading = false;
      });
    },
    /** 添加成员按钮操作 */
    handleAddUser() {
      this.loading = true;
      this.addUserDialog = true;
      getAlluser().then((response)=>{
        this.users=response.data;
        this.loading = false;
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.testplatfromProjectId != null) {
            updateProject(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");

              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 添加用户提交
    submitFormUserAdd(){
      this.userAdd.userIds=[];
      this.userAdd.userIds.unshift(this.userAdd.userId);
      const testplatfromProjectmemberAdd={};
      testplatfromProjectmemberAdd.userIds=this.userAdd.userIds;
      testplatfromProjectmemberAdd.testplatfromProjectId=this.testplatfromProjectId;
      addUser(testplatfromProjectmemberAdd).then((response)=>{
        this.addUserDialog=false;
        this.projectUserDialog=false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const testplatfromProjectIds = row.testplatfromProjectId;
      this.$modal
        .confirm(
          '是否确认删除项目管理编号为"' + testplatfromProjectIds + '"的数据项？'
        )
        .then(function () {
          return delProject(testplatfromProjectIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleDeleteUser(row){
      const id = row.id;
      this.$modal
        .confirm(
          '是否确认删除人员id为:"' + id + '"的数据项？'
        )
        .then(function () {
          return deletelUser(id);
        })
        .then(() => {
          const testplatfromProject = {};
          testplatfromProject.testplatfromProjectId = row.testplatfromProjectId;
          projectMemberPage(testplatfromProject).then((response) => {
            this.projectUserDialog = true;
            this.testplatfromProjectId=row.testplatfromProjectId;
            this.testplatfromProjectmemberList = response.data.records;
          });
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleDeleteUserBatch(){
      this.$modal
        .then(() =>{
          console.log("111111111111");
          console.log(this.userIds);
          return deletelUserBatch(this.userIds);
        })
        .then(() => {
          const testplatfromProject = {};
          testplatfromProject.testplatfromProjectId = row.testplatfromProjectId;
          projectMemberPage(testplatfromProject).then((response) => {
            this.projectUserDialog = true;
            this.testplatfromProjectId=row.testplatfromProjectId;
            this.testplatfromProjectmemberList = response.data.records;
          });
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/project/export",
        {
          ...this.queryParams,
        },
        `project_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  